package cn.xerllent.system.service.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import cn.xerllent.system.entity.Log;

import java.util.List;

@Repository
@Mapper
public interface LogMapper extends BaseMapper<Log> {

    @Delete("delete from sys_log where logtype = #{logtype}")
    void deleteByLogType(@Param("logtype") String logtype);

    @Select("<script>select l.id,l.createtime as createtime,l.description, l.requestip,l.address,u.nickname from sys_log l  " +
            " left join sys_user u on u.uid=l.uid where l.type=1 " +
            " <if test = \"nickname !=null\"> and u.nickname LIKE CONCAT('%',#{nickname},'%')</if> order by l.id desc</script>")
    List<Log> findAllByPageable(@Param("nickname") String nickname);

    @Select("select count(*) FROM (select requestip FROM sys_log where createtime between #{date1} and #{date2} GROUP BY requestip) as s")
    long findIp(@Param("date1") String date1, @Param("date2") String date2);
    
    @Select("select * from sys_log where username=#{username} and description='用户登录'  and logtype ='INFO' order by createtime desc limit 1,1")
    Log findLastLogInUserLog(@Param("username") String username);
}
